package cn.wanda.bi.sunshine.platform.server.service.statistics;

import cn.wanda.bi.sunshine.platform.server.bean.mapper.A06727AppArticleDetailMapper;
import cn.wanda.bi.sunshine.platform.server.bean.model.A06727AppArticleDetail;
import cn.wanda.bi.sunshine.platform.server.bean.model.A06727AppArticleDetailExample;
import cn.wanda.bi.sunshine.platform.server.bean.model.A06727AppPrivilegeShoppingPage;
import cn.wanda.bi.sunshine.platform.server.controller.response.common.ListResponse;
import cn.wanda.bi.sunshine.platform.server.controller.response.common.SimpleResponse;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Created by wangfupeng1
 */
@Service
public class A06727AppArticleDetailService {

	public static final Logger logger = LoggerFactory.getLogger(A06727AppArticleDetailService.class);

	@Autowired
	private A06727AppArticleDetailMapper a06727AppArticleDetailMapper;

	public ListResponse<A06727AppArticleDetail> articleDetail(String appoint, Long startTime, Long endTime,
			String articleNo, String articleTitle, String articleSource, String articleEditor, Integer index,
			Integer limit) {
		A06727AppArticleDetailExample a06727AppArticleDetailExample = new A06727AppArticleDetailExample();
		A06727AppArticleDetailExample.Criteria criteria = a06727AppArticleDetailExample.createCriteria();
		if (startTime != null)
			criteria.andArticlePublishDateGreaterThanOrEqualTo(new Date(startTime));
		if (endTime != null)
			criteria.andArticlePublishDateLessThanOrEqualTo(new Date(endTime));
		if (articleNo != null && StringUtils.isNotEmpty(articleNo))
			criteria.andArticleNoEqualTo(articleNo);
		if (articleTitle != null && StringUtils.isNotEmpty(articleTitle))
			criteria.andArticleTitleLike("%" + articleTitle + "%");
		if (articleSource != null && StringUtils.isNotEmpty(articleSource))
			criteria.andArticleSourceEqualTo(articleSource);
		if (articleEditor != null && StringUtils.isNotEmpty(articleEditor))
			criteria.andArticleEditorEqualTo(articleEditor);
		int count = a06727AppArticleDetailMapper.countByExample(a06727AppArticleDetailExample);
		a06727AppArticleDetailExample.setOffset(index * limit - limit);
		a06727AppArticleDetailExample.setLimit(limit);
		if ("appoint".equalsIgnoreCase(appoint)) {
			a06727AppArticleDetailExample.setOrderByClause("article_click_per desc");
		} else {
			a06727AppArticleDetailExample.setOrderByClause("article_no desc");
		}
		List<A06727AppArticleDetail> a06727AppArticleDetails = a06727AppArticleDetailMapper
				.selectByExample(a06727AppArticleDetailExample);
		return ListResponse.success(a06727AppArticleDetails, count, index, limit);
	}

    @Cacheable(value="cache:statistics", key = "'cache:statistics:articledetail:clickper:starttime:'+#startTime+':endtime:'+#endTime")
	public SimpleResponse articleClickPer(String articleTypeLabel, Long startTime, Long endTime) {
		A06727AppArticleDetailExample a06727AppArticleDetailExample = new A06727AppArticleDetailExample();
		A06727AppArticleDetailExample.Criteria criteria = a06727AppArticleDetailExample.createCriteria();
		criteria.andArticleTypeLabelEqualTo(articleTypeLabel);
		if (startTime != null)
			criteria.andArticlePublishDateGreaterThanOrEqualTo(new Date(startTime));
		if (endTime != null)
			criteria.andArticlePublishDateLessThanOrEqualTo(new Date(endTime));
		List<A06727AppArticleDetail> a06727AppArticleDetails = a06727AppArticleDetailMapper
				.selectByExample(a06727AppArticleDetailExample);
		int count = 0;
		Float temp = 0.0f;
		for (A06727AppArticleDetail a06727AppArticleDetail : a06727AppArticleDetails) {
			Float articleClickPer = a06727AppArticleDetail.getArticleClickPer();
			temp = temp + articleClickPer;
			count++;
		}
		if (count != 0) {
			temp = temp / count;
		}
		return SimpleResponse.success(temp);
	}
}
